package br.com.infocruz.seam.crm.ejb.ws;

import java.util.List;

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;
import org.jboss.wsf.spi.annotation.WebContext;

import br.com.infocruz.seam.crm.ejb.facade.CustomerFacade;
import br.com.infocruz.seam.crm.ejb.model.Customer;

@Name("customerService")
@Stateless
@WebService(name = "CustomerService", serviceName = "CustomerService")
@WebContext(contextRoot = "/seam-crm-ws", 
	urlPattern="/services/CustomerService")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
/*
 * * setProperty must be overridden by all subclasses of SOAPMessage JDK 1.6.0
 * https://jira.jboss.org/jira/browse/JBWS-1439 The final comment looks like a
 * workaround: Copy the three jars from <JBOSS_HOME>\server\default\lib to
 * <JBOSS_HOME>\lib\endorsed. jboss-jaxrpc.jar jboss-jaxws.jar jboss-saaj.jar
 */
public class CustomerService implements CustomerServiceRemote {

	@In
	Credentials credentials;
	
	@In
	Identity identity;
	
	@In(create = true)
	CustomerFacade customerFacade;

	@WebMethod
	public boolean login(String username, String password) {
		credentials.setUsername(username);
		credentials.setPassword(password);
		identity.login();
		return identity.isLoggedIn();
	}

	@WebMethod
	public boolean logout() {
		identity.logout();
		return !Identity.instance().isLoggedIn();
	}
	
	@WebMethod
	@Restrict("#{identity.loggedIn and s:hasRole('member') and s:hasPermission('customerService','findAll')}")
	public Customer[] listCustomers() {
		List<Customer> list = customerFacade.findAll();
		return list.toArray(new Customer[list.size()]);
	}

	@WebMethod
	@Restrict("#{identity.loggedIn and s:hasRole('member') and s:hasPermission('customerService','find')}")
	public Customer findCustomer(Long id) {
		return customerFacade.find(id);
	}

	@WebMethod
	@Restrict("#{identity.loggedIn and s:hasRole('admin') and s:hasPermission('customerService','create')}")
	public void createCustomer(Customer entity) {
		customerFacade.create(entity);
	}
	
	@WebMethod
	@Restrict("#{identity.loggedIn and s:hasRole('admin') and s:hasPermission('customerService','update')}")
	public void updateCustomer(Customer entity) {
//		if(entity != null && entity.getId() != null) {
//			Customer customer = customerFacade.find(entity.getId());
//			if(customer != null) {
//				Integer version = customer.getVersion();
//				entity.setVersion(version);
//			}
//		}
		customerFacade.update(entity);
	}

	@WebMethod
	@Restrict("#{identity.loggedIn and s:hasRole('admin') and s:hasPermission('customerService','remove')}")
	public void removeCustomer(Long customerId) {
		Customer customer = customerFacade.find(customerId);
		customerFacade.remove(customer);
	}

}
